﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace mirror
{
    class SkySphere
    {
        Effect mFx;
        TextureCube mTex;
        Model mSphere;

        public void Init()
        {
            GraphicsDevice device = cRenderManager.Instance.Device();
            ContentManager content = cRenderManager.Instance.Content();

            mFx = content.Load<Effect>("SkySphere");
            //TODO: mTex = content.Load<TextureCube>("cubemap");
            mSphere = content.Load<Model>("models/SphereHighPoly");

            foreach (ModelMesh mesh in mSphere.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    part.Effect = mFx;
                }
            }
        }

        public void Render()
        {
            GraphicsDevice device = cRenderManager.Instance.Device();
            // Set the View and Projection matrix for the effect
            mFx.Parameters["SkyboxTexture"].SetValue(
                mTex);
            mFx.Parameters["ViewMatrix"].SetValue(
                Camera.Instance.ViewMatrix);
            mFx.Parameters["ProjectionMatrix"].SetValue(
                Camera.Instance.ProjectionMatrix);
            // Draw the sphere model that the effect projects onto
            foreach (ModelMesh mesh in mSphere.Meshes)
            {
                mesh.Draw();
            }

            // Undo the renderstate settings from the shader
            device.RasterizerState = RasterizerState.CullCounterClockwise;
            device.DepthStencilState = DepthStencilState.Default;
        }
    }
}
